module.exports = {

	/**
	 * 添加模块历史记录
	 * @param {[type]} module [description]
	 */
	addModuleHistory(module) {
		// return
		let {
			store,
		} = this
		let moduleClone = $.extend(true, {}, module)
		moduleClone.operate = 'addModule'
		store.history[moduleClone.id] = moduleClone
	},

	/**
	 * 添加api历史记录
	 * @param {[type]} api [description]
	 */
	addApiHistory(api) {
		// return
		if (!api || !api.attributes)
			return false
		let {
			store,
		} = this
		let apiClone = $.extend(true, {}, api)
		let moduleId = apiClone.attributes.moduleId
		apiClone.operate = 'addApi'
		store.history[moduleId] = store.history[moduleId] || {
			id: moduleId,
			operate: 'addApi',
			apis: {}
		}
		store.history[moduleId].apis[apiClone.id] = apiClone
	},

	/**
	 * 删除模块/api节点历史记录
	 * @param  {[type]} node [description]
	 * @param  {[type]} type [description]
	 * @return {[type]}      [description]
	 */
	delNodeHistory(node, type) {
		// return
		if (!node || !node.id)
			return false
		let {
			store,
		} = this
		if (type == 'module') {
			let moduleDetail = store.history[node.id] = store.history[node.id] || {
				id: node.id
			}
			if (moduleDetail.operate == 'addApi') {
				delete store.history[node.id]
				return
			}
			moduleDetail.operate = 'delModule'
			return
		}
		let moduleId = node.attributes.moduleId
		let apiId = node.id
		store.history[moduleId] = store.history[moduleId] || {
			id: moduleId,
			operate: 'delApi',
			apis: {}
		}
		let apiDetail = store.history[moduleId].apis[apiId] = store.history[moduleId].apis[apiId] || {
			id: apiId,
		}
		if (apiDetail.operate == 'delApi') {
			delete store.history[moduleId].apis[apiId].operate
			return
		}
		apiDetail.operate = 'delApi'
	},

	nameModifyHistory(type, value, moduleId, apiId) {
		// return
		let {
			store,
		} = this
		let module = store.history[moduleId] = store.history[moduleId] || {
			id: moduleId,
			operate: 'apiModify',
			apis: {}
		}
		if (type == 'module') {
			module.text = value
		} else {
			let api = module.apis[apiId] = module.apis[apiId] || {
				id: apiId,
				operate: 'apiModify',
				operateAttributes: {},
				operateParameters: {},
			}
			api.text = value
		}
	},

	/**
	 * 接口详情修改历史记录
	 * @param  {[type]} detail    [description]
	 * @param  {[type]} attribute [description]
	 * @param  {[type]} newValue  [description]
	 * @param  {[type]} oldValue  [description]
	 * @return {[type]}           [description]
	 */
	apiDetailModifyHistory(attribute, newValue, oldValue) {
		// return
		let {
			store,
		} = this
		let module = checkApi(store, 'detail')
		if (!module)
			return false
		let apiId = store._currentEditApiDetail.id
		let api = module.apis[apiId];
		if (api.operateAttributes)
			api.operateAttributes[attribute] = newValue
	},

	/**
	 * 接口参数历史记录
	 * @param  {[type]} apiDetail [description]
	 * @param  {[type]} editRow   [description]
	 * @param  {[type]} type      [description]
	 * @return {[type]}           [description]
	 */
	apiParameterHistory(flag, editRow, type, attribute, newValue, oldValue) {
		// return
		let {
			store,
		} = this
		let module = checkApi(store, 'parameter')
		if (!module)
			return false
		let apiId = store._currentEditApiDetail.id
		let _treeId_ = editRow._treeId_
		let api = module.apis[apiId]
		let parameter = api.operateParameters[_treeId_] = api.operateParameters[_treeId_] || {
			_treeId_,
			name: {
				oldValue: editRow.name,
				newValue: editRow.name,
			},
			type: {
				oldValue: editRow.type,
				newValue: editRow.type,
			},
			content: {
				oldValue: editRow.content,
				newValue: editRow.content,
			},
			remark: {
				oldValue: editRow.remark,
				newValue: editRow.remark,
			},
			mandatory: {
				oldValue: editRow.mandatory,
				newValue: editRow.mandatory,
			},
		}
		if (type == 'del' && parameter.operate == 'add')
			delete api.operateParameters[_treeId_]
		if (type == 'modify') {
			oldValue = parameter.operate == 'add' ? newValue : oldValue
			parameter[attribute].oldValue = parameter[attribute].oldValue || oldValue
			parameter[attribute].newValue = newValue
		}
		if (parameter.operate == 'add')
			return false
		parameter.operate = type
		parameter.flag = flag
	},

	/**
	 * 导入JSON数据历史
	 * @param  {[type]} JSONData [description]
	 * @return {[type]}          [description]
	 */
	importJSONHistory(JSONData) {
		// return
		let {
			store,
		} = this
		let module = checkApi(store, 'parameter')
		if (!module)
			return false
		let apiId = store._currentEditApiDetail.id
		let api = module.apis[apiId]
		api.operateJSON = $.extend((api.operateJSON || {}), JSONData)
	},

	/**
	 * 导入模板数据
	 * @param  {[type]} template [description]
	 * @return {[type]}          [description]
	 */
	importTemplateHistory(template) {
		// return
		let {
			store,
		} = this
		let module = checkApi(store, 'parameter')
		if (!module)
			return false
		let apiId = store._currentEditApiDetail.id
		let api = module.apis[apiId]
		api.operateTemplate = template
	},

	/**
	 * 保存时和上一个版本比较差异
	 * @return {[type]} [description]
	 */
	diffHistory() {
		// return
		let {
			orgProjectData,
			history,
		} = this
		let orgModules = orgProjectData.modules
		let moduleKeyArr = Object.keys(history)
		let messageArr = []
		for (let i = 0; i < moduleKeyArr.length; i++) {
			let moduleId = moduleKeyArr[i]
			let module = history[moduleId]
			let msg = ''
			if (module.operate == 'addModule') {
				msg = `新建模块[${module.text}]`
				messageArr.push(msg)
				continue;
			} else if (module.operate == 'delModule') {
				msg = `删除模块[${module.text}]`
				messageArr.push(msg)
				continue;
			}
			let newModuleName = module.text
			let oldModuleName = orgModules[moduleId].text
			if (newModuleName && newModuleName != oldModuleName) {
				msg = `模块名修改[${oldModuleName} -->> ${newModuleName}]`
				messageArr.push(msg)
			}
			let apis = module.apis
			let apiKeyArr = Object.keys(apis)
			for (let j = 0; j < apiKeyArr.length; j++) {
				let key = apiKeyArr[j]
				let api = apis[key]
				if (api.operate == 'addApi') {
					msg = `新建接口[${api.text}]`
					messageArr.push(msg)
					continue;
				} else if (api.operate == 'delApi') {
					msg = `删除接口[${api.text}]`
					messageArr.push(msg)
					continue;
				}
				// api.operateAttributes[attribute]
				// 接口变更对比
				let oldApi = orgModules[moduleId].apis[api.id]
				let newApiName = api.text
				let oldApiName = oldApi.text
				newApiName = newApiName || oldApiName
				if (newApiName && newApiName != oldApiName) {
					msg = `接口名修改[${oldApiName} -->> ${newApiName}]`
					messageArr.push(msg)
				}
				let operateAttributes = api.operateAttributes
				let attrArr = Object.keys(operateAttributes)
				for (let k = 0; k < attrArr.length; k++) {
					let key = attrArr[k]
					let oldValue = oldApi.attributes[key]
					let newValue = operateAttributes[key]
					if (newValue != oldValue) {
						msg = `接口(${newApiName})属性[${key}]变更[${oldValue} -->> ${newValue}]`
						messageArr.push(msg)
					}
				}
				let operateParameters = api.operateParameters
				let paramArr = Object.keys(operateParameters)
				for (let n = 0; n < paramArr.length; n++) {
					let key = paramArr[n]
					let parameter = operateParameters[key]
					let flag = parameter.flag == 'req' ? '请求' : '响应'
					let oldValue = parameter.name.oldValue
					let newValue = parameter.name.newValue
					if (parameter.operate == 'add')
						msg = `接口(${newApiName})“${flag}”添加参数{(${newValue})类型:(${parameter.type.newValue})}`
					if (parameter.operate == 'del')
						msg = `接口(${newApiName})“${flag}”删除参数(${newValue})`
					messageArr.push(msg)
					if (parameter.operate == 'modify') {
						if (newValue != oldValue) {
							msg = `接口(${newApiName})“${flag}”参数名修改(${oldValue} --> ${newValue})`
							messageArr.push(msg)
						}
						oldValue = parameter.type.oldValue
						newValue = parameter.type.newValue
						if (newValue != oldValue) {
							msg = `接口(${newApiName})“${flag}”参数类型修改(${oldValue} --> ${newValue})`
							messageArr.push(msg)
						}
						oldValue = parameter.content.oldValue
						newValue = parameter.content.newValue
						if (newValue != oldValue) {
							msg = `接口(${newApiName})“${flag}”参数含义修改(${oldValue} --> ${newValue})`
							messageArr.push(msg)
						}
						oldValue = parameter.remark.oldValue
						newValue = parameter.remark.newValue
						if (newValue != oldValue) {
							msg = `接口(${newApiName})“${flag}”参数备注修改(${oldValue} --> ${newValue})`
							messageArr.push(msg)
						}
						oldValue = parameter.mandatory.oldValue
						newValue = parameter.mandatory.newValue
						if (newValue != oldValue) {
							msg = `接口(${newApiName})“${flag}”参数可选项修改(${oldValue} --> ${newValue})`
							messageArr.push(msg)
						}
					}
				}
				if (api.operateJSON) {
					// msg = `导入JSON数据${JSON.stringify(api.operateJSON)}`
					msg = `导入JSON数据`
					messageArr.push(msg)
				}
				if (api.operateTemplate) {
					msg = `导入模板数据[${api.operateTemplate}]`
					messageArr.push(msg)
				}
			}
		}
		return messageArr
	},
}

/**
 * 检查该接口是否为新建接口/删除接口
 * 新建/删除接口不需要记录详情
 * @param  {[type]}  store     [description]
 * @return {Boolean}           [description]
 */
function checkApi(store, type) {
	let apiDetail = store._currentEditApiDetail
	if (!apiDetail || !apiDetail.attributes)
		return false
	let moduleId = apiDetail.attributes.moduleId
	let apiId = apiDetail.id
	let module = store.history[moduleId]
	if (module && ((module.operate == 'addModule' && type == 'parameter') || module.operate == 'delModule'))
		return false
	let apisHistory = module && module.apis[apiId] || null
	if (apisHistory && ((apisHistory.operate == 'addApi' && type == 'parameter') || apisHistory.operate == 'delApi'))
		return false
	module = store.history[moduleId] = store.history[moduleId] || {
		id: moduleId,
		operate: 'apiModify',
		apis: {},
	}
	module.apis[apiId] = module.apis[apiId] || {
		id: apiId,
		operate: 'apiModify',
		operateAttributes: {},
		operateParameters: {},
		operateJSON: null,
		operateTemplate: '',
	}
	return module
}